package com.emily.test.service.impl;

import com.emily.test.mapper.RCompanyRoleMapper;
import com.emily.test.mapper.TResourceMapper;
import com.emily.test.model.RCompanyRole;
import com.emily.test.model.TResource;
import com.emily.test.service.TResourceService;
import com.emily.test.utils.ConstentUtil;
import com.emily.test.utils.ListUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service(value = "tResourceServiceAgent")
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public class TResourceServiceImpl implements TResourceService {

    private Logger logger = LoggerFactory.getLogger(TResourceServiceImpl.class);

    @Resource
    private TResourceMapper tResourceMapper;
    @Resource
    private RCompanyRoleMapper rCompanyRoleMapper;

    @Override
    public TResource selectByPrimaryKey(int id) {
        return tResourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertTResource(TResource tResource) {
        return tResourceMapper.insertSelective(tResource);
    }

    @Override
    public int updateAllTResourceInfo(TResource resource) {
        return tResourceMapper.updateByPrimaryKey(resource);
    }

    @Override
    public int updateSomeTResourceInfo(TResource resource) {
        return tResourceMapper.updateByPrimaryKeySelective(resource);
    }

    @Override
    public int deleteTResource(int id) {
        return tResourceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TResource> getResourceList() {
        return tResourceMapper.selectTResourceList();
    }

    @Override
    public int updateByPrimaryKeySelective(TResource tResource) {
        return tResourceMapper.updateByPrimaryKeySelective(tResource);
    }

    @Override
    public List<Map<String, Object>> selectTReSourceInfoListByRoleIds(List roleIds) {
        return tResourceMapper.selectTReSourceInfoListByRoleIds(roleIds);
    }

    @Override
    public List<Map<String, Object>> getIndexList(int id) {
        List<RCompanyRole> rCompanyRoles = rCompanyRoleMapper.selectRoleIdListByCompanyId(id);
        if(ListUtil.listIsNull(rCompanyRoles)){
            return null;
        }
        ArrayList<Integer> roleIds = new ArrayList<>();
        for (RCompanyRole rCompanyRole : rCompanyRoles) {
            roleIds.add(rCompanyRole.getRoleId());
        }
        List<Map<String, Object>> reList = tResourceMapper.selectTReSourceInfoListByRoleIds(roleIds);

        //一级菜单
        List<Map<String,Object>> parentResourceList=new ArrayList<Map<String,Object>>();
        //二级菜单
        List<Map<String,Object>> childResourceList=new ArrayList<Map<String,Object>>();

        //资源(菜单)按照pid分层
        for(Map<String, Object> reLi : reList){
            Integer pid=reLi.get("pid") ==null ? null: (Integer) reLi.get("pid");
            if(ConstentUtil.isEmpty(pid)){
                parentResourceList.add(reLi);
            }else{
                childResourceList.add(reLi);
            }
        }

        //给一级菜单添加二级菜单
        for( Map<String,Object> parentResourceMap :parentResourceList ){
            Integer parentId=parentResourceMap.get("resourceId") ==null ?  null : (Integer) parentResourceMap.get("resourceId");
            List<Map<String,Object>> childInParentList=new ArrayList<Map<String,Object>>();

            for(Map<String,Object> childResourceMap : childResourceList){
                Integer  childPId= childResourceMap.get("pid") ==null ? null :  (Integer) childResourceMap.get("pid");
               if(parentId.equals(childPId)){
                   childInParentList.add(childResourceMap);
               }
            }
            parentResourceMap.put("childResourceList",childInParentList);
        }
        return ListUtil.removeRepeatMapByKey(parentResourceList,"resourceId");
    }

    @Override
    public List<TResource> selectResourceListByCondition(TResource tResource) {
        if(!ConstentUtil.isEmpty(tResource)){
            //如果查询条件中没有state，则查询启用与禁用的信息列表
            if(ConstentUtil.isEmpty(tResource.getState())){
                return tResourceMapper.selectResourceListByConditionWithOutDel(tResource);
            }else{
                return tResourceMapper.selectResourceListByConditionWithInDel(tResource);
            }
        }
        return null;
    }

    @Override
    public int updateResourceInfoByIds(List<TResource> resourceList) {
        return tResourceMapper.updateResourceInfoByIds(resourceList);
    }

    @Override
    public List<Map<String, Object>> selectTReSourceInfoListByRoleIdIns(List list, String rName) {
        return tResourceMapper.selectTReSourceInfoListByRoleIdIns(list,rName);
    }
}
